package com.lcxw.hxzbadmin.controller.giftandcubecontroller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbadmin.entity.requestbean.AddGiftRequestBean;
import com.lcxw.hxzbadmin.entity.requestbean.ModOneGiftReqBean;
import com.lcxw.hxzbcommon.common.StringUtil;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbadmin.entity.requestbean.ModGiftRequestBean;
import com.lcxw.hxzbapi.entity.pojo.Gift;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.GiftService;

@RestController
@CrossOrigin
@RequestMapping("admin/gift")
public class GiftController {
    private Logger logger = LoggerFactory.getLogger(GiftController.class);

    @Resource
    GiftService giftService;

    @PostMapping("/getAll")
    public Response getAllGift(@RequestBody JSONObject requestParams){
        logger.info("admin {} getAll gift .",requestParams.getString("adminName"));

        Map<String, Object> data = new LinkedHashMap<>();
        List<Gift> giftList;

        try {
            giftList = giftService.selectAllGift();
            int count = giftList.size();
            data.put("count",count) ;
            data.put("giftList",giftList);
        } catch (Exception e) {
            return Response.dbFail(logger,"get all gift.");
        }

        return Response.ok(data);
    }

    @PostMapping("/mod")
    public Response modGiftList(@RequestBody ModGiftRequestBean modGiftRequestBean){
        logger.info("admin {} mod gift list",modGiftRequestBean.getAdminName());

        List<Gift> giftList = modGiftRequestBean.getGifts();

        try{
            for (Gift gift:giftList) {
                if (null == giftService.selectByPrimaryKey(gift.getId())){
                    giftService.insert(gift);
                }else {
                    giftService.updateByPrimaryKey(gift);
                }
            }
        }catch (Exception e){
            return Response.dbFail(logger,"operate gift list");
        }

        return Response.common(logger,"mod gift list success.",0,"success",null);

    }

    @PostMapping("/add")
    public Response addGiftList(@RequestBody AddGiftRequestBean addGiftRequestBean){
        logger.info("admin {} add a gift",addGiftRequestBean.getAdminName());

        Gift gift = addGiftRequestBean.getGift();
        gift.setAddTime(StringUtil.getDateTime());

        try {
            giftService.insert(gift);
        } catch (Exception e) {
            Response.dbFail(logger,"操作礼物数据库失败，请稍后再试");
        }

        return Response.common(logger,"mod gift list success.",0,"success",null);

    }

    @PostMapping("/delete")
    public Response deleteGift(@RequestBody JSONObject requestParams){
        int id = requestParams.getInteger("id");

        logger.info("admin {} delete gift {}.",requestParams.getString("adminName"),id);

        try {
            giftService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            return Response.dbFail(logger,"delete gift");
        }

        return Response.common(logger,"delete gift success.",0,"success",null);

    }

    @PostMapping("/modOne")
    public Response modOneGift(@RequestBody ModOneGiftReqBean requestParams){
        Gift gift = requestParams.getGift();

        logger.info("admin {} mod one gift {}.",requestParams.getAdminName(),gift.getId());

        try {
            giftService.updateByPrimaryKey(gift);
        } catch (Exception e) {
            return Response.dbFail(logger,"mod one gift");
        }

        return Response.common(logger,"delete gift success.",0,"success",null);

    }

    @PostMapping("/enable/set")
    public Response setGiftEnable(@RequestBody JSONObject requestBody){
        int id = requestBody.getInteger("id");
        int enable = requestBody.getInteger("enable");
        Gift gift;
        try {
            gift = giftService.selectByPrimaryKey(id);
            gift.setEnable(enable);
            giftService.updateByPrimaryKey(gift);
        } catch (Exception e) {
            return Response.dbFail(logger,"set gift enable");
        }

        return Response.ok();

    }

}
